package nancal.mp.mvc.dev


import io.swagger.annotations.Api
import io.swagger.annotations.ApiOperation
import nancal.mp.db.mongo.entity.dev.*
import nancal.mp.db.mongo.mor
import nbcp.base.annotation.*
import nbcp.base.annotation.mvc.*
import nbcp.base.comm.ApiResult
import nbcp.base.comm.JsonResult
import nbcp.base.comm.ListResult
import nbcp.base.comm.PagerModel
import nbcp.base.extend.*
import nbcp.base.json.*
import nbcp.mvc.sys.*
import nbcp.myoql.db.mongo.*

import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController
import javax.servlet.http.HttpServletRequest

/**
 * Created by CodeGenerator at 2021-04-12 12:10:50
 */
@Api(description = "Git服务器", tags = arrayOf("GitServer"))
@RestController
@RequestMapping("/dev/git-server")
class GitServerAutoController {

    @ApiOperation("列表")
    @PostMapping("/list")
    fun list(
        id: String, //当列表列新一条后，刷新时使用
        name: String,
        @org.springframework.web.bind.annotation.RequestBody query: PagerModel<Any?>,
        request: HttpServletRequest
    ): ListResult<GitServer> {

        mor.dev.gitServer.query()
                .apply {
                    if (id.HasValue) {
                        this.where { it.id mongoEquals id }
                    }
                    if (name.HasValue) {
                        this.where { it.name mongoLike name }
                    }
                }
                .withPager(query)
                .toListResult()
                .apply {
                    return this;
                }
    }

    @ApiOperation("详情")
    @PostMapping("/detail/{id}")
    fun detail(
            @Require id: String,
            request: HttpServletRequest
    ): ApiResult<GitServer> {
        mor.dev.gitServer.queryById(id)
                .toEntity()
                .apply {
                    if (this == null) {
                        return ApiResult.error<GitServer>("找不到数据")
                    }

                    return ApiResult.of(this)
                }
    }

    @ApiOperation("更新")
    @PostMapping("/save")
    fun save(
            @org.springframework.web.bind.annotation.RequestBody entity: GitServer,
            request: HttpServletRequest
    ): ApiResult<String> {
        //鉴权
        var userId = request.UserId;

        //如果有 code ， 则必须唯一

        if (entity.code.HasValue) {
            var codes = mor.dev.gitServer.query()
                    .where { it.code mongoEquals entity.code }
                    .limit(0, 2)
                    .toList();

            if (entity.id.isEmpty()) {
                if (codes.size > 0) {
                    throw RuntimeException("数据库存在 code 值：" + entity.code);
                }
            } else {
                if (codes.filter { it.id != entity.id }.size > 0) {
                    throw RuntimeException("数据库存在 code 值：" + entity.code);
                }
            }
        }

        mor.dev.gitServer.updateWithEntity(entity)

                .run {
                    if (entity.id.HasValue) {
                        return@run this.execUpdate()
                    } else {
                        return@run this.execInsert()
                    }
                }
                .apply {
                    if (this == 0) {
                        return ApiResult.error("更新失败")
                    }
                }



        mor.dev.gitData.update()
                .where { it.server.id mongoEquals entity.id }
                .set { it.server.url to entity.host }
                .set { it.server.name to entity.name }
                .exec()

        return ApiResult.of(entity.id)
    }

    @ApiOperation("删除")
    @PostMapping("/delete/{id}")
    fun delete(
            @Require id: String,
            request: HttpServletRequest
    ): JsonResult {
        //鉴权
        var userId = request.UserId

        var entity = mor.dev.gitServer.queryById(id).toEntity()
        if (entity == null) {
            return JsonResult.error("找不到数据")
        }

        mor.dev.gitServer.deleteById(id)
                .exec()
                .apply {
                    if (this == 0) {
                        return JsonResult.error("删除失败")
                    }
                    //实体上配置垃圾箱功能，可物理删除，会自动移到垃圾箱。
                    return JsonResult()
                }
    }
}
